//
//  TestOSX.cpp
//  NanoOSX
//
//  Created by Jeppe Nielsen on 9/7/13.
//  Copyright (c) 2013 Jeppe Nielsen. All rights reserved.
//

#include "Engine.hpp"
#include "OpenGL.hpp"
#include "RenderSystem.hpp"
#include "FirstPersonMoverSystem.hpp"
#include "SpriteMeshSystem.hpp"
#include "TouchSystem.hpp"
#include <iostream>
#include "Plane.hpp"
#include "FileReader.hpp"
using namespace Nano;
using namespace std;

class Tester : public GameState<Tester> {
    
    GameWorld world;
    RenderSystem* renderer;
    GameObject* camera;
    
    std::vector<GameObject*> list;
  
    void Initialize() {
        
        renderer = world.CreateSystem<RenderSystem>();
        world.CreateSystem<FirstPersonMoverSystem>()->Input = &Input;
        world.CreateSystem<TouchSystem>()->Input = &Input;
        world.CreateSystem<SpriteMeshSystem>();
        
        
        camera = world.CreateObject();
        camera->AddComponent<Camera>()->Viewport = Manager().Viewport();
        camera->AddComponent<Transform>()->Position = Vector3(0,0,4);
        cam = camera->GetComponent<Camera>();
        camera->AddComponent<FirstPersonMover>();
        
        string filename = FileReader::GetBundleDir() + "Gadget.xml";
        
        
        gadget = world.CreateObject();
        gadget->AddComponent<Transform>()->Position = Vector3(0,0,-0.05f);
        gadget->AddComponent<Mesh>();
        gadget->AddComponent<Material>()->LightingEnabled = false;
        gadget->AddComponent<Sizeable>()->Size = Vector2(3,3);
        gadget->AddComponent<Sprite>();
        gadget->AddComponent<Colorable>()->Color = Colour::Red();
        
        //gadget->Save(filename);
        //gadget = world.Load(filename);
        
        for (int x=0; x<20; x++) {
            for (int y=0; y<20; y++) {
                GameObject* clone = gadget->Clone();
                clone->GetComponent<Transform>()->Position = Vector3(x*3.1f,y*3.1f,0);
                clone->GetComponent<Colorable>()->Color = Colour::HslToRgb(x * y * 2, 0.3, 1.0, 1.0);
                list.push_back(clone);
            }
        }
        
        //gadget->GetComponent<Transform>()->Position = {-5,0,0};
        
        //world.Debug();
        
        time = 0;
    }
    
    float time;
    
    void Update(float dt) {
        world.Update(dt);
        
        time += dt;
        
        for (int i=0; i<list.size(); i++) {
            Vector3 pos = list[i]->GetComponent<Transform>()->Position;
            list[i]->GetComponent<Colorable>()->Color = Colour::HslToRgb(pos.x + pos.y *3 + time * 100, 0.5 + (0.5 + sin(time * 2) * 0.5) * 0.5, 1.0, 1.0);
        }
        
        if (gadget) {
            Plane plane(Vector3(0,0,1),0);
            float dist;
            Ray ray = cam->GetRay(camera->GetComponent<Transform>(), Input.GetTouchPosition(0));
            if (plane.IntersectsRay(ray, &dist)) {
                gadget->GetComponent<Transform>()->Position = ray.position + ray.direction * dist;
            }
        }
    }
    
    void Render() {
        glClearColor(0.0f, 0.0f, 0, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        
        renderer->Render();
    }
    
    GameObject* gadget;
    Camera* cam;
    
};

/*
int main() {
    Engine e;
    e.Start<Tester>();
    return 0;
}
*/
